package cn.lili.controller.statistics;

import cn.lili.common.aop.annotation.PreventDuplicateSubmissions;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.order.order.client.OrderFlowStatisticsClient;
import cn.lili.modules.statistics.entity.dto.GoodsStatisticsQueryParam;
import cn.lili.modules.statistics.entity.dto.StatisticsQueryParam;
import cn.lili.modules.statistics.entity.enums.StatisticsQuery;
import cn.lili.modules.statistics.entity.vo.*;
import cn.lili.modules.statistics.service.IndexStatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * 首页统计数据接口
 *
 * @author Bulbasaur
 * @since 2020/12/15 17:53
 */
@Slf4j
@Api(tags = "首页统计数据接口")
@RestController
@RequestMapping("/statistics/index")
@RequiredArgsConstructor
public class IndexStatisticsController {

    private final IndexStatisticsService indexStatisticsService;

    private final OrderFlowStatisticsClient orderFlowStatisticsClient;

    @PreventDuplicateSubmissions
    @ApiOperation(value = "获取首页查询数据")
    @GetMapping
    public ResultMessage<IndexStatisticsVO> index() {
        try {
            AuthUser authUser = Objects.requireNonNull(UserContext.getCurrentUser());
            if (!SceneEnums.MANAGER.equals(authUser.getScene())) {
                String storeId = authUser.getExtendId();
                return ResultUtil.data(indexStatisticsService.indexStatistics(storeId));
            }

            return ResultUtil.data(indexStatisticsService.indexStatistics(null));
        } catch (Exception e) {
            log.error("获取首页查询数据错误", e);
        }
        return null;
    }

    @ApiOperation(value = "获取首页查询热卖商品TOP10")
    @GetMapping("/goodsStatistics")
    public ResultMessage<List<GoodsStatisticsDataVO>> goodsStatistics(GoodsStatisticsQueryParam goodsStatisticsQueryParam) {

        //按照金额查询
        goodsStatisticsQueryParam.setType(StatisticsQuery.PRICE.name());
        return ResultUtil.data(indexStatisticsService.goodsStatistics(goodsStatisticsQueryParam));
    }

    @ApiOperation(value = "获取首页查询热卖店铺TOP10")
    @GetMapping("/storeStatistics")
    public ResultMessage<List<StoreStatisticsDataVO>> storeStatistics(StatisticsQueryParam statisticsQueryParam) {
        return ResultUtil.data(indexStatisticsService.storeStatistics(statisticsQueryParam));
    }

    @ApiOperation(value = "通知提示信息")
    @GetMapping("/notice")
    public ResultMessage<IndexNoticeVO> notice() {
        AuthUser authUser = Objects.requireNonNull(UserContext.getCurrentUser());
        if (!SceneEnums.MANAGER.equals(authUser.getScene())) {
            String storeId = authUser.getExtendId();
            return ResultUtil.data(indexStatisticsService.indexNotice(storeId));
        }
        return ResultUtil.data(indexStatisticsService.indexNotice(null));
    }

    @ApiOperation(value = "获取统计列表,排行前一百的数据")
    @GetMapping("/top100")
    public ResultMessage<List<GoodsStatisticsDataVO>> getByPage(GoodsStatisticsQueryParam statisticsQueryParam) {
        String storeId = Objects.requireNonNull(UserContext.getCurrentUser()).getExtendId();
        statisticsQueryParam.setStoreId(storeId);
        return ResultUtil.data(orderFlowStatisticsClient.getGoodsStatisticsData(statisticsQueryParam, 100));
    }

    @ApiOperation(value = "获取首页查询数据")
    @GetMapping("/store")
    public ResultMessage<StoreIndexStatisticsVO> storeIndex() {
        String storeId = Objects.requireNonNull(UserContext.getCurrentUser()).getExtendId();
        return ResultUtil.data(indexStatisticsService.storeIndexStatistics(storeId));
    }

    @PreventDuplicateSubmissions
    @ApiOperation(value = "获取首页查询数据")
    @GetMapping(("/urban"))
    public ResultMessage<StoreIndexStatisticsVO> urban() {
        try {
            return ResultUtil.data(indexStatisticsService.indexStatisticsByUrban(null));
        } catch (Exception e) {
            log.error("获取首页查询数据错误", e);
        }
        return null;
    }

    @ApiOperation(value = "城市分站通知提示信息")
    @GetMapping("/noticeByUrban")
    public ResultMessage<IndexNoticeVO> noticeByUrban() {
        return ResultUtil.data(indexStatisticsService.indexNoticeByUrban(null));
    }
}
